#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <math.h>
#include <qtextcodec.h>//
#include <stdio.h>
#include <QTextCodec>
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    QTextCodec::setCodecForTr(QTextCodec::codecForName("utf8"));
////    QTextCodec::setCodecForLocale(QTextCodec::codecForName("utf8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("utf8"));
    people_number=0;
    people_page_now=0;
    people_page_num=0;
    user_number=0;
    administrator_number=0;

//    ui->label->setText(tr("a用户名")));
    hide_other();
    open_file();
    read_people_msg_file();
    read_user_msg_file();
    read_administrator_msg_file();
    people_page_now = 0;
}
void MainWindow::open_file() //创建或者打开表格
{
    people_msg_file = fopen("people_msg.txt", "a+");                //打开表格
    user_msg_file = fopen("user_msg.txt", "a+");                    //打开表格
    administrator_msg_file = fopen("administrator_msg.txt", "a+");  //打开表格
//    printf("%d %d %d \n", people_msg_file,user_msg_file,administrator_msg_file);
    if (people_msg_file < 0 || user_msg_file < 0 || administrator_msg_file < 0)

        exit(0);
}
void MainWindow::hide_other()
{
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->tableWidget->hide();
    ui->tableWidget_2->hide();
    ui->tableWidget_3->hide();
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    ui->pushButton_7->hide();
    ui->pushButton_8->hide();
    ui->pushButton_9->hide();
    ui->pushButton_10->hide();
    ui->pushButton_11->hide();
    ui->pushButton_12->hide();
    ui->pushButton_13->hide();
    ui->pushButton_14->hide();
    ui->pushButton_15->hide();
    ui->pushButton_16->hide();
    ui->pushButton_17->hide();
    ui->pushButton_18->hide();
    ui->pushButton_19->hide();
    ui->label_5->hide();
    ui->label_4->hide();
    ui->label_6->hide();
    ui->label_7->hide();
    ui->label_8->hide();
    ui->label_9->hide();
    ui->label_10->hide();
    ui->lineEdit_3->hide();
    ui->lineEdit_4->hide();
    ui->lineEdit_5->hide();
    ui->lineEdit_6->hide();
    ui->lineEdit_7->hide();
}
void MainWindow::read_people_msg_file()
{
//    struct  People_info people_msg_temp;
    char name[20];
    char brith[20];
    char sexual[10];
    char major1[30];
    char speciality[30];
    char temp = '1';
    const char* zerostring = "";
    int i = 0;
    for (i = 0; i < 1000; i++)
    {
        fscanf(people_msg_file,"%s %s %s %s %s",name,brith,sexual,major1,speciality);
//        fscanf(people_msg_file,"%c",temp);
//        temp = fgetwc(people_msg_file);
        if (strlen(name) == 0)
            break;
        strcpy(All_people[i].name,name);
        strcpy(All_people[i].brith,brith);
        strcpy(All_people[i].sexual,sexual);
        strcpy(All_people[i].major1,major1);
        strcpy(All_people[i].speciality,speciality);
//        printf("%s %s %s %s %s\n", All_people[i].name, All_people[i].brith, All_people[i].sexual,All_people[i].major1, All_people[i].speciality);
//        fprintf(user_msg_file,"%s %s %s %s %s\n", All_people[i].name, All_people[i].brith, All_people[i].sexual,All_people[i].major1, All_people[i].speciality);
        strcpy(name, zerostring);
        strcpy(brith, zerostring);
        strcpy(sexual, zerostring);
        strcpy(major1, zerostring);
        strcpy(speciality, zerostring);
    }
    people_number = i;
    people_page_num = int((i-1) / 10);

}
void MainWindow::show_people_msg_table()
{
    clean_people__msg_table();
    for (int i = 0; i < 10; i++)
    {
        if (people_page_now * 10 + i >= people_number)
            break;
        ui->tableWidget->setItem(i,0,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].name)));
        ui->tableWidget->setItem(i,1,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].brith)));
        ui->tableWidget->setItem(i,2,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].sexual)));
        ui->tableWidget->setItem(i,3,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].major1)));
        ui->tableWidget->setItem(i,4,new QTableWidgetItem(QString(All_people[people_page_now * 10 + i].speciality)));
//        ui->tableWidget->item(i, 0)->setText(QString(QLatin1String(All_people[people_page_now * 10 + i].name)));
    }
    set_current_peopleMsg_page();

}
void MainWindow::clean_people__msg_table()
{
    for (int i = 0; i < 10; i++)
    {
        for ( int j = 0; j < 5; j++)
            ui->tableWidget->setItem(i,j,new QTableWidgetItem(""));
    }
}
void MainWindow::delet_oneperson()
{
    int curRow = ui->tableWidget->currentRow();

    for (int i = 0; i < 5; i++)
    {
        ui->tableWidget->setItem(curRow,i,new QTableWidgetItem(""));
    }
    for (int i = people_page_now * 10 + curRow; i < people_number; i++)
    {
        strcpy(All_people[i].name,All_people[i+1].name);
        strcpy(All_people[i].brith,All_people[i+1].brith);
        strcpy(All_people[i].sexual,All_people[i+1].sexual);
        strcpy(All_people[i].major1,All_people[i+1].major1);
        strcpy(All_people[i].speciality,All_people[i+1].speciality);
    }
    people_number -= 1;
    people_page_num = int((people_number-1) / 10);
//        printf("%d\n", people_page_num);
    if (people_page_num < people_page_now)
    {
        people_page_now = people_page_num;
    }
    write_people_msg_to_file();
    show_people_msg_table();
    set_current_peopleMsg_page();
}

void MainWindow::add_oneperson()
{
    QString name_Qstring = ui->tableWidget_2 ->item(0,0)->text();
    if (!name_Qstring.isEmpty() )
    {
        QString brith_Qstring = ui->tableWidget_2->item(0,1)->text();
        QString sexual_Qstring = ui->tableWidget_2->item(0,2)->text();
        QString major1_Qstring = ui->tableWidget_2->item(0,3)->text();
        QString speciality_Qstring = ui->tableWidget_2->item(0,4)->text();

        QByteArray name;
        QByteArray brith;
        QByteArray sexual;
        QByteArray major1;
        QByteArray speciality;
        name.append(name_Qstring);     //也可以 ba2 = s2.toLatin1();
        brith.append(brith_Qstring);
        sexual.append(sexual_Qstring);
        major1.append(major1_Qstring);
        speciality.append(speciality_Qstring);

        strcpy(All_people[people_number].name,name.data());
        strcpy(All_people[people_number].brith,brith.data());
        strcpy(All_people[people_number].sexual,sexual.data());
        strcpy(All_people[people_number].major1,major1.data());
        strcpy(All_people[people_number].speciality,speciality.data());
        people_number += 1;
        people_page_num = int((people_number-1) / 10);
        people_page_now = people_page_num;
//            printf("%d %d\n",people_number, people_page_num);
        write_people_msg_to_file();
        show_people_msg_table();
        set_current_peopleMsg_page();
    }
//    printf("1\n");

}

void MainWindow::people_masg_next_page()
{
    if ( people_page_now != people_page_num)
    {
        clean_people__msg_table();
        people_page_now += 1;
        show_people_msg_table();
        set_current_peopleMsg_page();
    }
}
void MainWindow::people_masg_last_page()
{
    if ( people_page_now != 0)
    {
        clean_people__msg_table();
        people_page_now -= 1;
        show_people_msg_table();
        set_current_peopleMsg_page();
    }
}
void MainWindow::people_masg_end_page()
{
    clean_people__msg_table();
    people_page_now = people_page_num;
    show_people_msg_table();
    set_current_peopleMsg_page();
}
void MainWindow::people_masg_first_page()
{
    clean_people__msg_table();
    people_page_now = 0;
    show_people_msg_table();
    set_current_peopleMsg_page();
}

void MainWindow::people_masg_page_jump()
{
    QString text_info = ui->lineEdit->text();
    QString text_info2 = ui->lineEdit_2->text();
    QByteArray text;
    QByteArray text2;
    text.append(text_info);     //也可以 ba2 = s2.toLatin1();
    text2.append(text_info2);
    const char *ID = text.data();
    const char *password = text2.data();

    QString Jump_page_Qstring = ui->lineEdit_3->text();
    QByteArray Jump_page_text = Jump_page_Qstring.toLatin1();
    char* Jump_page = Jump_page_text.data();
//    printf("%s\n", Jump_page);
    int page_num = char_to_int(Jump_page);
//    printf("qwe%d\n", page_num);
//    clean_people__msg_table();
    people_page_now = page_num-1;
    show_people_msg_table();
    set_current_peopleMsg_page();
}

void MainWindow::write_people_msg_to_file()
{
    fclose(people_msg_file);
    people_msg_file = fopen("people_msg.txt", "w+");
    for (int i = 0; i < 1000; i++)
    {
        if (strlen(All_people[i].name) == 0)
            break;
        fprintf(people_msg_file,"%s %s %s %s %s\n", All_people[i].name, All_people[i].brith, All_people[i].sexual,All_people[i].major1, All_people[i].speciality);
    }
    fclose(people_msg_file);
    people_msg_file = fopen("people_msg.txt", "a+");


}
void MainWindow::save_edited_people_masg()
{
    for (int j = 0; j < 10; j++)
    {
        QString name_Qstring = ui->tableWidget->item(j,0)->text();
        QString brith_Qstring = ui->tableWidget->item(j,1)->text();
        QString sexual_Qstring = ui->tableWidget->item(j,2)->text();
        QString major1_Qstring = ui->tableWidget->item(j,3)->text();
        QString speciality_Qstring = ui->tableWidget->item(j,4)->text();
        QByteArray name;
        QByteArray brith;
        QByteArray sexual;
        QByteArray major1;
        QByteArray speciality;
        name.append(name_Qstring);     //也可以 ba2 = s2.toLatin1();
        brith.append(brith_Qstring);
        sexual.append(sexual_Qstring);
        major1.append(major1_Qstring);
        speciality.append(speciality_Qstring);
        strcpy(All_people[people_page_now*10 + j].name,name.data());
        strcpy(All_people[people_page_now*10 + j].brith,brith.data());
        strcpy(All_people[people_page_now*10 + j].sexual,sexual.data());
        strcpy(All_people[people_page_now*10 + j].major1,major1.data());
        strcpy(All_people[people_page_now*10 + j].speciality,speciality.data());
    }
    write_people_msg_to_file();
    show_people_msg_table();
}
void MainWindow::set_current_peopleMsg_page()
{
    char people_page_num_str[10];
    char people_page_now_str[10];
    sprintf(people_page_num_str,"/%d 页",people_page_num+1);
    sprintf(people_page_now_str,"%d",people_page_now+1);
    ui->label_5->setText(QString(people_page_num_str));
    ui->lineEdit_3->setText(QString(people_page_now_str));
//    printf("%d %d %s\n",people_number, people_page_num, people_page_num_str);
}

void MainWindow::on_click_manager_people_msg()
{
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->tableWidget->show();
    ui->tableWidget_2->show();
    ui->pushButton_5->show();
    ui->pushButton_6->show();
    ui->pushButton_8->show();
    ui->pushButton_9->show();
    ui->pushButton_10->show();
    ui->pushButton_11->show();
    ui->pushButton_12->show();
    ui->pushButton_13->show();
    ui->pushButton_14->show();
    ui->label_4->show();
    ui->label_5->show();
    ui->lineEdit_3->show();
    people_page_now = 0;
    set_current_peopleMsg_page();
    show_people_msg_table();

}
void MainWindow::back_to_main_menu()
{
    ui->pushButton_3->show();
    ui->pushButton_4->show();
    ui->tableWidget->hide();
    ui->tableWidget_2->hide();
    ui->tableWidget_3->hide();
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    ui->pushButton_8->hide();
    ui->pushButton_9->hide();
    ui->pushButton_10->hide();
    ui->pushButton_11->hide();
    ui->pushButton_12->hide();
    ui->pushButton_13->hide();
    ui->pushButton_14->hide();
    ui->pushButton_15->hide();
    ui->pushButton_16->hide();
    ui->pushButton_17->hide();
    ui->pushButton_18->hide();
    ui->pushButton_19->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->label_6->hide();
    ui->label_7->hide();
    ui->label_8->hide();
    ui->label_9->hide();
    ui->label_10->hide();
    ui->lineEdit_3->hide();
    ui->lineEdit_4->hide();
    ui->lineEdit_5->hide();
    ui->lineEdit_6->hide();
    ui->lineEdit_7->hide();
}





void MainWindow::read_user_msg_file()
{
    char ID[40];
    char user_password[40];
    const char* zerostring = "";
    int i = 0;
    for (i = 0; i < 8; i++)
    {
        fscanf(user_msg_file,"%s %s", ID, user_password);
        if (strlen(ID) == 0)
            break;
        strcpy(All_user[i].ID,ID);
        strcpy(All_user[i].user_password,user_password);
//        printf("%s %s\n", All_user[i].ID, All_user[i].user_password);
        strcpy(ID, zerostring);
        strcpy(user_password, zerostring);
    }
    user_number = i;
}
void MainWindow::read_administrator_msg_file()
{
    char ID[40];
    char user_password[40];
    const char* zerostring = "";
    int i = 0;
    for (i = 0; i < 8; i++)
    {
        fscanf(administrator_msg_file,"%s %s", ID, user_password);
        if (strlen(ID) == 0)
            break;
        strcpy(All_administrator[i].ID,ID);
        strcpy(All_administrator[i].user_password,user_password);
//        printf("%s %s\n", All_administrator[i].ID, All_administrator[i].user_password);
        strcpy(ID, zerostring);
        strcpy(user_password, zerostring);
    }
    administrator_number = i;
}

void MainWindow::show_users_table()
{
    clean_users_table();
    for (int i = 0; i < 8; i++)
    {
        if (i >= administrator_number)
            break;
        ui->tableWidget_3->setItem(i,0,new QTableWidgetItem(QString(All_administrator[i].ID)));
    }
    for (int i = 0; i < 8; i++)
    {
        if (i >= user_number)
            break;
//        printf("%d \n", user_number);
        ui->tableWidget_3->setItem(i,1,new QTableWidgetItem(QString(All_user[i].ID)));
//        ui->tableWidget->item(i, 0)->setText(QString(QLatin1String(All_people[people_page_now * 10 + i].name)));
    }
}
void MainWindow::add_user()
{
    QString ID_Qstring = ui->lineEdit_6->text();
    if (!ID_Qstring.isEmpty() && user_number < 8)
    {
        QString user_password_Qstring = ui->lineEdit_7->text();
        QByteArray ID;
        QByteArray user_password;

        ID.append(ID_Qstring);     //也可以 ba2 = s2.toLatin1();
        user_password.append(user_password_Qstring);

        strcpy(All_user[user_number].ID,ID.data());
        strcpy(All_user[user_number].user_password,user_password.data());

        user_number += 1;
//            printf("%d %d\n",people_number, people_page_num);
        write_user_msg_to_file();
        show_users_table();
    }
}
void MainWindow::add_administrator()
{
    QString ID_Qstring = ui->lineEdit_4->text();
    if (!ID_Qstring.isEmpty() && administrator_number < 8)
    {
        QString user_password_Qstring = ui->lineEdit_5->text();
        QByteArray ID;
        QByteArray user_password;

        ID.append(ID_Qstring);     //也可以 ba2 = s2.toLatin1();
        user_password.append(user_password_Qstring);

        strcpy(All_administrator[administrator_number].ID,ID.data());
        strcpy(All_administrator[administrator_number].user_password,user_password.data());
\
        administrator_number += 1;
//            printf("%d %d\n",people_number, people_page_num);
        write_administrator__msg_to_file();
        show_users_table();
    }
}
void MainWindow::delet_user()
{
    int curRow = ui->tableWidget_3->currentRow();
    int curColumn = ui->tableWidget_3->currentColumn();
    ui->tableWidget_3->setItem(curRow,curColumn,new QTableWidgetItem(""));
    if (curColumn == 1)
    {
        for (int i = curRow; i < user_number; i++)
        {
            strcpy(All_user[i].ID,All_user[i+1].ID);
            strcpy(All_user[i].user_password,All_user[i+1].user_password);
        }
        user_number -= 1;
        write_user_msg_to_file();

    }
    else if (curColumn == 0)
    {
        for (int i = curRow; i < administrator_number; i++)
        {
            strcpy(All_administrator[i].ID,All_administrator[i+1].ID);
            strcpy(All_administrator[i].user_password,All_administrator[i+1].user_password);
        }
        administrator_number -= 1;
        write_administrator__msg_to_file();

    }
    show_users_table();
}

void MainWindow::clean_users_table()
{
    for (int i = 0; i < 8; i++)
    {
        for ( int j = 0; j < 2; j++)
            ui->tableWidget_3->setItem(i,j,new QTableWidgetItem(""));
    }
}
void MainWindow::write_user_msg_to_file()
{
    fclose(user_msg_file);
    user_msg_file = fopen("user_msg.txt", "w+");
    for (int i = 0; i < 8; i++)
    {
        if (i >= user_number)
            break;
        if (strlen(All_user[i].ID) == 0)
            break;
        fprintf(user_msg_file,"%s %s\n", All_user[i].ID, All_user[i].user_password);
    }
    fclose(user_msg_file);
    user_msg_file = fopen("user_msg.txt", "a+");
}
void MainWindow::write_administrator__msg_to_file()
{
    fclose(administrator_msg_file);
    administrator_msg_file = fopen("administrator_msg.txt", "w+");
    for (int i = 0; i < 8; i++)
    {
        if (i >= administrator_number)
            break;
        if (strlen(All_administrator[i].ID) == 0)
            break;
        fprintf(administrator_msg_file,"%s %s\n", All_administrator[i].ID, All_administrator[i].user_password);
    }
    fclose(administrator_msg_file);
    administrator_msg_file = fopen("user_msg.txt", "a+");
}


void MainWindow::on_click_edit_user_and_administrator_msg()
{
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->tableWidget_3->show();
    ui->lineEdit_4->show();
    ui->lineEdit_5->show();
    ui->lineEdit_6->show();
    ui->lineEdit_7->show();
    ui->pushButton_15->show();
    ui->pushButton_16->show();
    ui->pushButton_17->show();
    ui->pushButton_18->show();
    ui->label_6->show();
    ui->label_7->show();
    ui->label_8->show();
    ui->label_9->show();
    ui->label_10->show();
    show_users_table();
}

void MainWindow::on_click_view_people_msg()
{
    ui->pushButton_6->hide();
    ui->pushButton_3->hide();
    ui->pushButton_4->hide();
    ui->tableWidget->show();
//    ui->tableWidget_2->show();
    ui->pushButton_5->show();
    ui->pushButton_6->show();
    ui->pushButton_7->hide();
//    ui->pushButton_8->show();
//    ui->pushButton_9->show();
//    ui->pushButton_10->show();
    ui->pushButton_11->show();
    ui->pushButton_12->show();
    ui->pushButton_13->show();
    ui->pushButton_19->show();
//    ui->pushButton_14->show();
//    ui->label_4->show();
    ui->label_5->show();
    ui->lineEdit_3->show();
    people_page_now = 0;
    set_current_peopleMsg_page();
    show_people_msg_table();

}
void MainWindow::normal_user_back_to_main_menu()
{

    ui->tableWidget->hide();
//    ui->tableWidget_2->show();
    ui->pushButton_5->hide();
    ui->pushButton_6->hide();
    ui->pushButton_7->show();
//    ui->pushButton_8->show();
//    ui->pushButton_9->show();
    ui->pushButton_10->hide();
    ui->pushButton_11->hide();
    ui->pushButton_12->hide();
    ui->pushButton_13->hide();
    ui->pushButton_19->hide();
//    ui->pushButton_14->show();
//    ui->label_4->show();
    ui->label_5->hide();
    ui->lineEdit_3->hide();
    people_page_now = 0;
}
MainWindow::~MainWindow()
{
    delete ui;
    fclose(people_msg_file);
    fclose(user_msg_file);
    fclose(administrator_msg_file);
}

void MainWindow::affirm_user_msg()
{
    QString text_info = ui->lineEdit->text();
    QString text_info2 = ui->lineEdit_2->text();
    QByteArray text;
    QByteArray text2;
    text.append(text_info);     //也可以 ba2 = s2.toLatin1();
    text2.append(text_info2);
    const char *ID = text.data();
    const char *password = text2.data();
//    printf("%s\n", password);
    int i = 0;
    for (i = 0; i < user_number; i++)
    {
//        printf("21\n");
        if ( !strcmp(ID,All_user[i].ID) && !strcmp(password,All_user[i].user_password))
        {
//             printf("11\n");
            ui->lineEdit->close();
            ui->lineEdit_2->close();
            ui->label->close();
            ui->label_2->close();
            ui->label_3->close();
            ui->pushButton->close();
            ui->pushButton_2->close();
            ui->pushButton_7->show();
            break;
        }
    }
    if (i == user_number)
//         printf("12\n");
        ui->label_3->setText(tr("ID/Password error!"));
}

void MainWindow::affirm_administrator_msg()
{
//    ui->pushButton_3->show();
//    ui->pushButton_4->show();
    QString text_info = ui->lineEdit->text();
    QString text_info2 = ui->lineEdit_2->text();
    QByteArray text;
    QByteArray text2;
    text.append(text_info);     //也可以 ba2 = s2.toLatin1();
    text2.append(text_info2);
    const char *ID = text.data();
    const char *password = text2.data();
//    printf("%s %s %d \n", password,ID, administrator_number);
    int i = 0;
    for (i = 0; i < administrator_number; i++)
    {
//        printf("%s %s\n",All_administrator[i].user_password, All_administrator[i].ID);
        if ( !strcmp(ID,All_administrator[i].ID) && !strcmp(password,All_administrator[i].user_password))
        {
//            printf("11\n");
            ui->lineEdit->close();
            ui->lineEdit_2->close();
            ui->label->close();
            ui->label_2->close();
            ui->label_3->close();
            ui->pushButton->close();
            ui->pushButton_2->close();
            ui->pushButton_3->show();
            ui->pushButton_4->show();
            break;
        }
    }
    if ( i == administrator_number)
//        printf("12\n");
        ui->label_3->setText(QString("ID/Password error!"));
}



int MainWindow::char_to_int(const char* data)
{
    int Key_Buf = 0;
    for (int i = 0; i < strlen(data); i++)
    {
        switch(data[strlen(data)-i-1])
        {
            case '0':Key_Buf += 0 * pow(10, i);break;
            case '1':Key_Buf += 1 * pow(10, i);break;
            case '2':Key_Buf += 2 * pow(10, i);break;
            case '3':Key_Buf += 3 * pow(10, i);break;
            case '4':Key_Buf += 4 * pow(10, i);break;
            case '5':Key_Buf += 5 * pow(10, i);break;
            case '6':Key_Buf += 6 * pow(10, i);break;
            case '7':Key_Buf += 7 * pow(10, i);break;
            case '8':Key_Buf += 8 * pow(10, i);break;
            case '9':Key_Buf += 9 * pow(10, i);break;
            default:

                return -1;
        }
    }
    return Key_Buf;
}
